import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _forEach from "lodash/forEach";
var _excluded = ["components", "filterComponentScript", "scripts"];

/**
 * 用于方法类型的组件编辑方法
 * FunctionSelect.js
 * @date 2018/11/15
 */
import React from 'react';
import { Select } from 'antd';

var FunctionSelect = /*#__PURE__*/function (_React$Component) {
  _inherits(FunctionSelect, _React$Component);

  var _super = _createSuper(FunctionSelect);

  function FunctionSelect() {
    _classCallCheck(this, FunctionSelect);

    return _super.apply(this, arguments);
  }

  _createClass(FunctionSelect, [{
    key: "render",
    value: function render() {
      var _this$props = this.props,
          components = _this$props.components,
          filterComponentScript = _this$props.filterComponentScript,
          scripts = _this$props.scripts,
          otherProps = _objectWithoutProperties(_this$props, _excluded);

      return /*#__PURE__*/React.createElement(Select, otherProps, getComponentScripts(components, filterComponentScript), getPageScripts(scripts));
    }
  }]);

  return FunctionSelect;
}(React.Component);
/**
 * 通过 components 获取 components 的 Option.Group
 * @param {object} components
 * @param {function} filterScript
 */


FunctionSelect.defaultProps = {
  components: [],
  filterComponentScript: function filterComponentScript() {
    return true;
  },
  scripts: []
};
export { FunctionSelect as default };
export function getComponentScripts() {
  var components = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var filterScript = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {
    return true;
  };
  var optionGroups = [];

  _forEach(components, function (component) {
    var optGroupProps = {
      label: component.description,
      key: component.templateCode
    };
    var options = [];

    switch (component.templateType) {
      case 'DynamicForm':
        if (filterScript(component, 'submit') !== false) {
          options.push( /*#__PURE__*/React.createElement(Select.Option, {
            key: "this.ref[".concat(component.templateCode, "].submit"),
            value: "this.ref[".concat(component.templateCode, "].submit")
          }, "submit"));
        }

        if (filterScript(component, 'query') !== false) {
          options.push( /*#__PURE__*/React.createElement(Select.Option, {
            key: "this.ref[".concat(component.templateCode, "].query"),
            value: "this.ref[".concat(component.templateCode, "].query")
          }, "query"));
        }

        break;

      case 'DynamicTable':
        if (filterScript(component, 'reload') !== false) {
          options.push( /*#__PURE__*/React.createElement(Select.Option, {
            key: "this.ref[".concat(component.templateCode, "].reload"),
            value: "this.ref[".concat(component.templateCode, "].reload")
          }, "reload"));
        }

        if (filterScript(component, 'load') !== false) {
          options.push( /*#__PURE__*/React.createElement(Select.Option, {
            key: "this.ref[".concat(component.templateCode, "].load"),
            value: "this.ref[".concat(component.templateCode, "].load")
          }, "load"));
        }

        break;

      default:
        break;
    }

    if (options.length > 0) {
      optionGroups.push( /*#__PURE__*/React.createElement(Select.OptGroup, optGroupProps, options));
    }
  });

  return optionGroups;
}
export function getPageScripts() {
  var scripts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var options = [];

  _forEach(scripts, function (script) {
    options.push( /*#__PURE__*/React.createElement(Select.Option, {
      value: "this.".concat(script.name),
      key: "this.".concat(script.name)
    }, script.name));
  });

  return /*#__PURE__*/React.createElement(Select.OptGroup, {
    label: "\u9875\u9762\u811A\u672C"
  }, options);
}